package com.fancy.projecty.config.shiro;

import com.fancy.projecty.controller.LoginController;
import com.fancy.projecty.mapper.WaterAgentMapper;
import com.fancy.projecty.pojo.WaterAgent;
import com.fancy.projecty.pojo.WaterMenu;
import com.fancy.projecty.pojo.WaterRole;
import com.fancy.projecty.pojo.bo.LoginInfoBO;
import com.fancy.projecty.service.WaterRoleService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.crypto.hash.HashService;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Role;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

@Configuration
public class CustomRealm extends AuthorizingRealm {

	@Autowired
	private WaterAgentMapper waterAgentMapper;

//	@Autowired
//	private WaterSatffService waterSatffService;
//	@Autowired
//	private WaterMenuService waterMenuService;

	@Autowired
	private WaterRoleService waterRoleService;

	/**
	 * 进行授权的业务逻辑
	 * https://blog.csdn.net/beguile/article/details/88175023
	 * 根据菜单进行拦截功能  ,通过登录名 获取菜单功能
	 *
	 * @param principalCollection
	 * @return
	 */

	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {


//		//这里可以获取登陆信息
		WaterAgent waterAgent = (WaterAgent) getAvailablePrincipal(principalCollection);
//		String agentUsername = waterAgent.getAgentUsername();
		Integer agentId = waterAgent.getAgentId();
		SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
		List<WaterRole> roleList = this.waterRoleService.selectRoleAndMenuByLoginId(agentId);
		HashSet<String> roleSet = new HashSet<>();
		HashSet<String> permissionsSet = new HashSet<>();

		//查询用户所有的角色
		//查询用户所有的菜单
		for (WaterRole waterRole : roleList) {
			roleSet.add(waterRole.getRoleName());
			List<WaterMenu> waterMenuList = waterRole.getWaterMenuList();
			for (WaterMenu waterMenu : waterMenuList) {
				permissionsSet.add(waterMenu.getPermission());
			}
		}

		info.setRoles(roleSet);
		info.setStringPermissions(permissionsSet);


		return info;
	}

	/**
	 * 认证
	 *
	 * @return
	 * @throws AuthenticationException
	 */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		//1.获取用户输入的账号
		String username = (String) token.getPrincipal();

		WaterAgent waterAgent = this.waterAgentMapper.selectAgentByUserName(username);
		//进行认证操作
		if (waterAgent != null) {
			return getAuthenticationInfoByAgent(waterAgent);
		}

		return null;
	}

	/**
	 * 进行认证操作
	 *
	 * @param waterAgent
	 * @return
	 */
	private AuthenticationInfo getAuthenticationInfoByAgent(WaterAgent waterAgent) {

		if (waterAgent.getAgentIsdel() == 1) {
			throw new AuthenticationException("该账号已经被禁用");

		}
		LoginInfoBO loginInfoBO = new LoginInfoBO();
		loginInfoBO.setLoginId(waterAgent.getAgentId());
		loginInfoBO.setLoginName(waterAgent.getAgentUsername());
		LoginController.loginInfoThreadLocal.set(loginInfoBO);

		SimpleAuthenticationInfo simpleAuthenticationInfo =
				new SimpleAuthenticationInfo(waterAgent, waterAgent.getAgentPassword(), getName());

		return simpleAuthenticationInfo;
	}


}